﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:Toolbar runat=server></{0}:Toolbar>")> _
    Public Class Toolbar
        Inherits ControlsBase



        Private Sub Toolbar_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim sTemp As String = String.Empty
            'pt = New Forums.Utils.TimeCalcItem("ForumDisplay")
            If Not ControlConfig Is Nothing Then
                Dim obj As Object = DataCache.CacheRetrieve(ControlConfig.InstanceId & "aftb")
                If obj Is Nothing Then
                    sTemp = ParseTemplate()
                Else
                    sTemp = CType(obj, String)
                End If
                sTemp = Utilities.LocalizeControl(sTemp)
                If Not sTemp.Contains(Globals.ControlRegisterAFTag) Then
                    sTemp = Globals.ControlRegisterAFTag & sTemp
                End If
                Dim ctl As Control = Page.ParseControl(sTemp)
                LinkControls(ctl.Controls)
                Me.Controls.Add(ctl)
            End If
        End Sub
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                If TypeOf ctrl Is Controls.ControlsBase Then
                    DirectCast(ctrl, Controls.ControlsBase).ControlConfig = Me.ControlConfig
                End If
                If TypeOf ctrl Is Controls.Link Then
                    DirectCast(ctrl, Controls.Link).UserRoles = ForumUser.UserRoles
                End If
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
        Private Function ParseTemplate() As String
            Dim tb As String = DisplayTemplate 'Utilities.ParseToolBar(DisplayTemplate, PageId, InstanceId, UserId, CurrentUserTypes.Admin)
            'tb = tb.Replace
            tb = tb.Replace("[AF:TB:Unanswered]", "<af:link id=""lnkUnanswered"" NavigateUrl=""" & Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ViewType & "=grid", "afgt=unanswered"}) & """ text=""[RESX:Unanswered]"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:ActiveTopics]", "<af:link id=""lnkActive"" NavigateURL=""" & Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ViewType & "=grid", "afgt=activetopics"}) & """ text=""[RESX:ActiveTopics]"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:Search]", "<af:link id=""lnkSearch"" NavigateUrl=""" & Utilities.NavigateUrl(PageId, "", ParamKeys.ViewType & "=search") & """ text=""[RESX:Search]"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:Forums]", "<af:link id=""lnkForums"" navigateUrl=""" & Utilities.NavigateUrl(PageId) & """ text=""[RESX:FORUMS]"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:NotRead]", "<af:link id=""lnkNotRead"" NavigateUrl=""" & Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ViewType & "=grid", "afgt=notread"}) & """ text=""[RESX:NotRead]"" AuthRequired=""True"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:MyTopics]", "<af:link id=""lnkMyTopics"" NavigateUrl=""" & Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ViewType & "=grid", "afgt=mytopics"}) & """ text=""[RESX:MyTopics]"" AuthRequired=""True"" runat=""server"" />")
            tb = tb.Replace("[AF:TB:MyProfile]", String.Empty)
            tb = tb.Replace("[AF:TB:MemberList]", String.Empty)
            tb = tb.Replace("[AF:TB:MySettings]", String.Empty)
            tb = tb.Replace("[AF:TB:ControlPanel]", "<af:link id=""lnkControlPanel"" NavigateUrl=""" & Utilities.NavigateUrl(PageId, "EDIT", "mid=" & ControlConfig.InstanceId) & """ EnabledRoles=""" & ControlConfig.AdminRoles & """ Text=""[RESX:ControlPanel]"" runat=""server"" />")
            'TODO: Check for moderator
            tb = tb.Replace("[AF:TB:ModList]", String.Empty)
            DataCache.CacheStore(ControlConfig.InstanceId & "aftb", tb)
            Return tb
        End Function
    End Class

End Namespace
